/*
 * Stub.cpp
 *
 *  Created on: 03/02/2014
 *      Author: Bruno
 */
#include "DppmParams.h"
#include "DppmBuffer.h"
#include "DppmProcess.h"
#include "TestDppmProcess.h"
#include <iostream>

using namespace std;

void initDppm(DppmParams * &dppm, DppmBuffer &dppmBuff) {

	/*
	 Grafo de actividades

	 	 	  (a2)
	 	 	 /	 \
			/	  \
		(a1)	  (a4)
	 	 	\	  /
	 	 	 \   /
	 	 	 (a3)--(a5)
	 */

	int numAct = 5;
	int T = 25;

	int * numPreds = new int[5];
	numPreds[0] = 0; //a1
	numPreds[1] = 1; //a2
	numPreds[2] = 1; //a3
	numPreds[3] = 2; //a4
	numPreds[4] = 1; //a5

	int ** Preds = new int*[numAct];
	Preds[0] = NULL;
	Preds[1] = new int[numPreds[1]];
	Preds[2] = new int[numPreds[2]];
	Preds[3] = new int[numPreds[3]];
	Preds[4] = new int[numPreds[4]];

	Preds[1][0] = 0;
	Preds[2][0] = 0;
	Preds[3][0] = 1;
	Preds[3][1] = 2;
	Preds[4][0] = 2;

	int * numModes = new int[numAct];
	numModes[0] = 3;
	numModes[1] = 5;
	numModes[2] = 6;
	numModes[3] = 3;
	numModes[4] = 4;

	int **ModesCost = new int*[numAct];
	int **ModesTime = new int*[numAct];
	for (int i = 0; i < numAct; i++) {
		ModesCost[i] = new int[numModes[i]];
		ModesTime[i] = new int[numModes[i]];
	}

	// (cost,time)
	ModesCost[0][0] = 2;	ModesTime[0][0] = 8;	//(2,8)
	ModesCost[0][1] = 3;	ModesTime[0][1] = 5;	//(3,5)
	ModesCost[0][2] = 7;	ModesTime[0][2] = 1; 	//(7,1)

	ModesCost[1][0] = 1;	ModesTime[1][0] = 10; 	//(1,10)
	ModesCost[1][1] = 5;	ModesTime[1][1] = 8; 	//(5,8)
	ModesCost[1][2] = 6;	ModesTime[1][2] = 4; 	//(6,4)
	ModesCost[1][3] = 10;	ModesTime[1][3] = 2; 	//(10,2)
	ModesCost[1][4] = 15;	ModesTime[1][4] = 1; 	//(15,1)

	ModesCost[2][0] = 3;	ModesTime[2][0] = 15; 	//(3,15)
	ModesCost[2][1] = 5;	ModesTime[2][1] = 14; 	//(5,14)
	ModesCost[2][2] = 8;	ModesTime[2][2] = 11; 	//(8,11)
	ModesCost[2][3] = 15;	ModesTime[2][3] = 9; 	//(15,9)
	ModesCost[2][4] = 17;	ModesTime[2][4] = 7; 	//(17,7)
	ModesCost[2][5] = 25;	ModesTime[2][5] = 3; 	//(25,3)

	ModesCost[3][0] = 4;	ModesTime[3][0] = 10;	//(4,10)
	ModesCost[3][1] = 5;	ModesTime[3][1] = 9; 	//(5,9)
	ModesCost[3][2] = 7;	ModesTime[3][2] = 7; 	//(7,7)

	ModesCost[4][0] = 7;	ModesTime[4][0] = 12;	//(7,12)
	ModesCost[4][1] = 9;	ModesTime[4][1] = 11;	//(9,11)
	ModesCost[4][2] = 12;	ModesTime[4][2] = 8; 	//(12,8)
	ModesCost[4][3] = 13;	ModesTime[4][3] = 7; 	//(10,7)

	dppm = new DppmParams(numAct, T, numPreds, Preds, numModes, ModesCost,ModesTime);

	dppmBuff.init(NULL, numAct, 0);

	Tuple * t = new Tuple(numAct);
	//inserto las primeras tuplas
	for (int i = 0; i < 3; i++) {
		t->modes(0,i);
		t->lastAct(0);
		t->times(0,ModesTime[0][i]);
		t->cost(ModesCost[0][i]);
		dppmBuff.push(t->copy());
	}

	delete t;

	delete[] numPreds;
	delete[] numModes;
	for (int i = 0; i < numAct; i++) {
		delete[] Preds[i];
		delete[] ModesCost[i];
		delete[] ModesTime[i];
	}
	delete[] Preds;
	delete[] ModesCost;
	delete[] ModesTime;

}

void doBacktracking(DppmParams &dppm, DppmBuffer &dppmBuff, Tuple * &tupleSol) {

	//hago el backtracking sin concurrencia
	if (dppmBuff.isEmpty()) {
		cout << "BUFFER VACIO!!!" << endl;
	}
	while (!dppmBuff.isEmpty()) {
		ProcessTuple(dppm, dppmBuff, tupleSol);
	}

}
